package com.example.dormitory_system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_system.domain.dto.student.MaintenanceAddDTO;
import com.example.dormitory_system.domain.enetiy.Maintenance;
import com.example.dormitory_system.domain.vo.student.MaintainVO;
import com.example.dormitory_system.service.MaintenanceService;
import com.example.dormitory_system.mapper.MaintenanceMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author keenan
* @description 针对表【tb_maintenance(维修申请表)】的数据库操作Service实现
* @createDate 2025-03-26 15:30:48
*/
@Service
public class MaintenanceServiceImpl extends ServiceImpl<MaintenanceMapper, Maintenance>
    implements MaintenanceService {
    @Autowired
    private MaintenanceMapper maintenanceMapper;

    @Override
    public List<MaintainVO> listMaintain(Long studentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return maintenanceMapper.listMaintain(studentId);
    }

    @Override
    public int addMaintain(MaintenanceAddDTO maintenanceAddDTO) {
        Maintenance maintenance = new Maintenance();
        BeanUtil.copyProperties(maintenanceAddDTO, maintenance);
        maintenance.setStatus(0);
        maintenance.setApplyDate(LocalDateTime.now());
        return maintenanceMapper.insert(maintenance);
    }

    @Override
    public List<MaintainVO> sysListMaintain(Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return maintenanceMapper.sysListMaintain(status);
    }

    @Override
    public int updateMaintain(Long maintenanceId, Integer status) {
        System.out.println(maintenanceId);
        System.out.println(status);
        return maintenanceMapper.update(new LambdaUpdateWrapper<Maintenance>().set(Maintenance::getStatus, status)
                .eq(Maintenance::getMaintenanceId, maintenanceId)
                .eq(Maintenance::getStatus, 0));
    }
}